package cn.lyl.org.controller;

import cn.lyl.basic.util.ExcelUtils;
import cn.lyl.org.domain.ShopAuditLog;
import cn.lyl.org.dto.ShopDto;
import cn.lyl.org.service.IShopService;
import cn.lyl.org.domain.Shop;
import cn.lyl.org.query.ShopQuery;
import cn.lyl.basic.util.AjaxResult;
import cn.lyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (RuntimeException e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }


    /**
     * 入驻
     * @param shop
     * @return
     */
    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }


    /**
     * 审核失败
     * @param log
     * @return
     */
    @PostMapping(value="/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log){
        try {
            shopService.auditReject(log);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }


    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping(value="/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log){
        try {
            shopService.auditPass(log);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
     * 导出
     * @param response
     */
    @GetMapping(value="/export")
    public void exportData( HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        //调用工具类导出excel
        //(List<?> list, 导出的数据
        // String title, 标题
        // String sheetName, sheetName页名称
        // Class<?> pojoClass,导出的类型
        // String fileName,文件名
        // HttpServletResponse response)响应对象
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        ExcelUtils.exportExcel(shops, null, "店铺数据", Shop.class, "系统数据", response);
    }

    /**
     * 导入
     * @param file
     */
   @PostMapping("/import")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，MultipartFile file, 文件对象
        // Integer titleRows, 忽略标题行数
        // Integer headerRows, 护理表头行数
        // Class<T> pojoClass，生成的对象类型
        List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
       list.forEach(System.out::println);
    }

    @GetMapping(value="/active/{id}")
    public String active(@PathVariable("id")Long id){
        try {
            shopService.active(id);
            return "激活成功";
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "激活失败";
        }catch (Exception e) {
            e.printStackTrace();
            return ("网络繁忙，请稍后重试");
        }
    }


    @GetMapping("/echarts")
    public List<ShopDto> echarts() {
        return shopService.echarts();
    }




}
